<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

UserBean user = (UserBean)request.getSession().getAttribute("userBean");
String _db = user.getDbType();

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>" />
    <title>用户组信息</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="expires" content="0" />
	
	<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
	<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
	<link type="text/css" rel="stylesheet" href="resource/css/style.css" />

	<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
	<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
	<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="js/validata.js"></script>
	
	<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
	<script type="text/javascript" src="ligerUI/js/plugins/ligerResizable.js"></script>
    <script type="text/javascript" src="ligerUI/js/plugins/ligerDialog.js" ></script>
    <script type="text/javascript" src="ligerUI/js/plugins/ligerButton.js"></script>
  </head>
  <script type="text/javascript">			
		_jsonObj.classname = "com.wxtf.platform.plugins.group.GroupAction";
		_jsonObj.methodname = "queryPage";
		_jsonObj.extenddata = '[{"caption":0}]';
		var _db = '<%=_db%>';
		var manager ="";
		 $(function(){
				manager = $("#group-grid").ligerGrid({
					url: _url, 
					parms: _jsonObj,enabledSort:false,
					dataAction:'server',method:'post',
					width: '100%', height:'100%',
	                usePager:false,
	                alternatingRow: true,//奇偶行显示效果
	                tree: { columnId: 'text1' },//treeGrid效果列
	                columns: [
	                    { display: 'id', name: 'id', width: 1,hide: 1 },
	                    { display: 'pid', name: 'gparent_id', width: 1,hide: 1 },
	                    { display: 'code', name: 'logic_code' , width: 1,hide: 1 },
	                    { display: '名称', name: 'text',id:'text1',width: 350, align: 'left'},
	                    { display: '角色数', name: 'role_num',  width: 1,hide:true, align: 'left' },
	                	{ display: '人数', name: 'user_num', width: 1,hide:true, align: 'left' },
	                	{ display: '新建',name:'add', width:80,render:function(record, index, value, oth){
	                			return '<a href="javascript:add(\'' + record.id + '\')" class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/add.gif"/></a>';
	                		}
	                	},
	                	{ display: '编辑', name:'edit',width:80,render:function(record, index, value, oth){
	                		  if(record.id!=0){
	                			return '<a href="javascript:edit(\'' + record.id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/edit.gif"/></div>';
	                		}}
	                	},
	                	{ display: '删除', name:'delete',width:80,render:function(record, index, value, oth){
	                		 if(record.id!=0){	
	                			return '<a href="javascript:delData(\'' + record.id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/delete.gif"/></div>';
	                		}}
	                	},
	                	{ display: '选择角色', name: 'action', width: 80,render:function(record, index, value, oth){
                			if(record.id==0){
                				return '';
                			}
                				return '<a href="javascript:open_group_role(\'' + record.id + '\')" class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/role.gif"/></a>';
	                		} },
	                	{ display: '选择人员', name: 'isQuick', width: 80,render:function(record, index, value, oth){
	                			if(record.id==0){
	                				return '';
	                			}
	                			return '<a href="javascript:open_group_user(\'' + record.id + '\')" class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/memeber.gif"/></a>';
	                		}
	                	}
	                ]
	               
	            });
	           
	        });
	        //页面刷新方法
	        function reload(){
	        	_jsonObj.methodname = "queryPage";
				_jsonObj.extenddata = '[{"id":0}]';
				manager.loadData();
	        }
	        //添加方法
	        var add_dialog;
	        function add(row){	       
	        var row = manager.getSelectedRow();
	         $("#goods input[type='text']").each(function() {
					        $(this).val("");
				        });	
			   add_dialog= initWindowByDiv("goods","添加窗口",400,240,save_,closeWin);
			     
			   $("#pid").attr("value",row.id);
			   $("#pname").attr("value",row.text);
			}
			
			function save_(item, dialog){
				var id=$("#id").attr("value");
				var caption=$("#caption").attr("value");
				var logic_code=$("#group_code").attr("value");
				var gparent_id=$("#pid").attr("value");
				if(fucCheckLength(caption.trim()) <= 0 || fucCheckLength(caption.trim()) > 50){
					warningTip("名称：不能为空且不能超过50字符");
					return;
				}
				if(!checkIsNum(logic_code.trim())){
					warningTip("排序号：必须为整数");
					return;
				}
				if(Math.abs(parseInt(logic_code)) > 1000000){
					warningTip("排序号：不能超过1000000");
					return;
				}
			    _jsonObj.methodname = "save";
		        _jsonObj.logicdata = "[{\"group_id\":\"" +id + "\",\"gparent_id\":\""+ gparent_id +"\",\"caption\":\""+ caption +"\",\"logic_code\":\""+ logic_code +"\",\"action\":\"\"}]"
	             add_dialog.hide();
	             callserver(_jsonObj,sucess,error);
			}
			//编辑方法
			var upd_dialog;
			function edit(record){
			    var Parent_row = manager.getParent(manager.getSelectedRow());//获取父级信息
			        $("#pname").attr("value",Parent_row.text);
				var edit_row = manager.getSelectedRow();
			    upd_dialog= initWindowByDiv("goods","编辑窗口",400,240,update_,closeWin);
			    $("#id").attr("value",edit_row.id);
			    $("#pid").attr("value",edit_row.gparent_id);
			    $("#caption").attr("value",edit_row.text);
			    $("#group_code").attr("value",edit_row.logic_code);
			}
			
			function update_(item, dialog){
		        var id=$("#id").attr("value");
				var caption=$("#caption").attr("value");
				var logic_code=$("#group_code").attr("value");
				var gparent_id=$("#pid").attr("value");
			    _jsonObj.methodname = "update";
		        _jsonObj.logicdata = "[{\"group_id\":\"" +id + "\",\"gparent_id\":\""+ gparent_id +"\",\"caption\":\""+ caption +"\",\"logic_code\":\""+ logic_code +"\",\"action\":\"\"}]"
				 document.forms[0].reset();
				 upd_dialog.hide();
                callserver(_jsonObj, sucess, error);
			}
			//删除方法
			function delData(record){
				var row = manager.getSelectedRowObj();
	            if(manager.hasChildren(row)){
	            var message="您现在正在删除的节点包含子\n节点删除后子节点将一并被删除";
	                $.ligerDialog.confirm(message, function (yes, value){
                         if(yes){
                            del_();
                         }else{
                            return;
                         }
                     }); 
	              }else{
					var messages="您确认删除吗？该操作不可恢复";
	                $.ligerDialog.confirm(messages, function (yes, value){
                         if(yes){
                            del_();
                         }else{
                            return;
                         }
                     }); 
		                }
			}
			function del_(item, dialog){
			   var del_row = manager.getSelectedRow();
			   _jsonObj.methodname = "delete";
			   _jsonObj.logicdata = "[{\"ids\":\"" +del_row.id + "\"}]"
		       callserver(_jsonObj, sucess, error);
			}
		///////////////////////////////////////////////对组选角色的操作///////////////////////////////////////////////////////////////////////////////	
			//组分配用：显示角色信息
			   var list_role_jsonObj = { 
				"classname": "com.wxtf.platform.plugins.group.GroupAction",
				"methodname": "listRole",
				"logicdata": [],
				"extenddata": []
			};
			
			 //组分配用：显示组中角色信息
	            var group_role_list_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "listRole4Group",
					"logicdata": [],
					"extenddata": []
				};
			//打开组选择角色窗口
			var role_group_dialog;
			var group_role;
			var check_roles;
			var check_Grole_id;
		function open_group_role(record){
			role_group_dialog=initWindowByDiv2('group_role', '角色选择窗口', 790, 435, close_rolediv);
			check_Grole_id=record;
			//获取选中行组信息
			  //组分配用：显示角色信息
			list_role_jsonObj.logicdata = "[{\"groupId\":\"" +check_Grole_id + "\",\"roleName\":\"\"}]";
			group_role = $("#role_div").ligerGrid({
					url: _url, 
					parms: list_role_jsonObj,
					width: "350",
					height: "325",
					async:false,
					limit:10,
					dataAction:'server',method:'post',
					rownumbers:true,
					checkbox: true,
	                usePager:true,
	                alternatingRow: true,//奇偶行显示效果
	                isChecked:function(record, index, value, oth){//判断是否显示被选中
			                 	  if(record.group_id==check_Grole_id){
			                        return true;
			                 	    }
	                 	  },
	                columns: [
	                 { display: '', name: 'group_id',hide:1},
	                    { display: '角色ID', name: 'role_id',hide:1},
	                	{ display: '可选角色', name: 'caption',width: 159}
	                ]
	            });
	            
	            //组分配用：显示组中角色信息
				group_role_list_jsonObj.extenddata = "[{\"id\":\"" +check_Grole_id + "\",\"roleName\":\"\"}]";
				check_roles = $("#check_role").ligerGrid({
						url: _url, 
						parms: group_role_list_jsonObj,
						width: "350",
						height: "325",
						async:false,
						limit:10,
						dataAction:'server',method:'post',
						rownumbers:true,
						checkbox: true,//复选框
		                usePager:true,//分页
		                alternatingRow: true,//奇偶行显示效果
		                columns: [ { display: '角色ID', name: 'role_id',hide:1},
		                	{ display: '已选角色', name: 'caption', width: '159'}
		                ]
		            });
			}
			//向组中添加角色
			function save_group_role(){
			    var save_role = manager.getSelectedRow();//获取选中的组信息
				var save_roles = group_role.getCheckedRows();//获取选中的角色信息
				
	        	var str_role = "";
	        	 $(save_roles).each(function (){
	        	     str_role += this.role_id + ",";
	        	            });
	        	if(str_role == ''){
	        	 	warningTip('请选择右侧用户');
	        	 	return;
	        	 }
	                 str_role=str_role.substring(0,str_role.length-1);
	             var save_group_role_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "saveRole",
					"logicdata": [],
					"extenddata": []
				};
			     save_group_role_jsonObj.logicdata = "[{\"groupId\":\""+save_role.id+"\",\"roleId\":\""+str_role+"\"}]";
			      callserver(save_group_role_jsonObj, sucess_grole, error);
			}
			//从组中删除角色
			function del_group_role(){
			    var del_role = manager.getSelectedRow();//获取组信息
				var del_roles = check_roles.getCheckedRows();//获取选中的角色信息
	        	var role_str = "";
	        	 $(del_roles).each(function (){
	        	     role_str += this.role_id + ",";
	        	            });
	        	 if(role_str==''){
	        	    warningTip('请选择左侧用户');
	        	 	return;
	        	 }
	                role_str=role_str.substring(0,role_str.length-1);
	             var del_group_role_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "delRole",
					"logicdata": [],
					"extenddata": []
				};
			     del_group_role_jsonObj.logicdata = "[{\"groupId\":\""+del_role.id+"\",\"roleId\":\""+role_str+"\"}]";
			     callserver(del_group_role_jsonObj, sucess_grole, error);
			}
			//查询角色
			function search_role(){
			   var role_name=$("#role_name").attr("value");		
			     list_role_jsonObj.logicdata = "[{\"groupId\":\""+check_Grole_id+"\",\"roleName\":\""+role_name+"\"}]";
			     callserver(list_role_jsonObj, sucess_search_role, error);
			}
			//查询组中角色
			function search_group_role(){
			   var user_name=$("#group_role_name").attr("value");
			     group_role_list_jsonObj.extenddata = "[{\"id\":\""+check_Grole_id+"\",\"roleName\":\""+user_name+"\"}]";
			     callserver(group_role_list_jsonObj, sucess_search_grole, error);
			}
		///////////////////////////////////////////////////对组选用户的操作///////////////////////////////////////////////////////////////////////////	
			//显示人员信息
				var list_user_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "listUser",
					"logicdata": [],
					"extenddata": []
				};
			//显示组中人员信息
				var group_user_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "listUser4Group",
					"logicdata": [],
					"extenddata": []
				};
			
			//打开组选择人员窗口
			var user_group_dialog;
			var group_user;
			var check_users;
			var check_group_id;
			function open_group_user(record){
				check_group_id=record;
				
				//显示窗口
	           user_group_dialog = initWindowByDiv2('group_user', '人员选择窗口', 800, 450, close_userdiv);
				
				
				//选中行的组信息
				var gu_row = manager.getSelectedRow();
				//显示人员信息
				list_user_jsonObj.extenddata = "[{\"groupId\":\"" +gu_row.id + "\",\"userName\":\"\",\"jigou\":\"\"}]";
				group_user = $("#user_div").ligerGrid({
						url: _url, 
						parms: list_user_jsonObj,
						width: "350",
						height: "325",
						async:false,
						limit:10,
						dataAction:'server',method:'post',
						checkbox: true,rownumbers:true, usePager:true,
		                alternatingRow: true,//奇偶行显示效果
		                isChecked:function(record, index, value, oth){
							if(record.group_id==gu_row.id){
			                	return true;
			            	}
						},
						groupColumnName:'jigou',
	                	groupColumnDisplay:'机构',
		                columns: [
		                    { display: '', name: 'group_id',hide:true,width: 1},
		                    { display: '人员ID', name: 'user_id',hide:1,width: 1},
		                	{ display: '可选用户', name: 'caption',width: 200},
		                	{ display: '机构', name: 'jigou', align: 'left', hide: 1 ,width: 1}
		                ]
		            });
				//显示组中人员信息
				group_user_jsonObj.extenddata = "[{\"id\":\"" +gu_row.id + "\",\"userName\":\"\",\"jigou\":\"\"}]";
				check_users = $("#check_user").ligerGrid({
						url: _url, 
						parms: group_user_jsonObj,
						limit:10,
						width: "350",
						height: "325",
						async:false,
						dataAction:'server',method:'post',
						checkbox: true,rownumbers:true,
		                usePager:true,
		                alternatingRow: true,//奇偶行显示效果
		                groupColumnName:'jigou',
	                	groupColumnDisplay:'机构',
		                columns: [ { display: '人员ID', name: 'user_id', hide:1 },
		                	{ display: '已选用户', name: 'caption', width: '200',align: 'left' },
		                	{ display: '机构', name: 'jigou', align: 'left', hide: 1 ,width: 1}
		                ]
		            });
			}
			//向组中添加用户
			function save_group_user(){
			    var save_row = manager.getSelectedRow();
				var save_rows = group_user.getCheckedRows();
	        	var user_str = "";
	        	 $(save_rows).each(function (){
	        	     user_str += this.user_id + ",";
	        	 });
	        	 if(user_str == ''){
	        	 	warningTip('请选择右侧用户');
	        	 	return;
	        	 }
	             user_str=user_str.substring(0,user_str.length-1);
	             var save_group_jsonObj = { 
					"classname": "",
					"methodname": "",
					"logicdata": [],
					"extenddata": []
				};
				 save_group_jsonObj.classname = "com.wxtf.platform.plugins.group.GroupAction";
                 save_group_jsonObj.methodname = "saveUser";
			     save_group_jsonObj.logicdata = "[{\"groupId\":\""+save_row.id+"\",\"userId\":\""+user_str+"\"}]";
			      callserver(save_group_jsonObj, sucess_guser, error);
			}
			//从组中删除用户
			function del_group_user(){
			    var del_row = manager.getSelectedRow();
				var del_rows = check_users.getCheckedRows();
	        	var str = "";
	        	 $(del_rows).each(function (){
	        	     str += this.user_id + ",";
     	         });
     	         if(str == ''){
	        	 	warningTip('请选择左侧用户');
	        	 	return;
	        	 }
	             str=str.substring(0,str.length-1);
	              var del_group_jsonObj = { 
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "delUser",
					"logicdata": [],
					"extenddata": []
				};
			     del_group_jsonObj.logicdata = "[{\"groupId\":\""+del_row.id+"\",\"userId\":\""+str+"\"}]";
			     callserver(del_group_jsonObj, sucess_guser, error);
			}
			//查询用户
			function serach_user(){
			   var user_name=$("#user_query").attr("value");
			   var jigou=$("#user_query_jigou").attr("value");
			     list_user_jsonObj.extenddata = "[{\"groupId\":\""+check_group_id+"\",\"userName\":\""+user_name+"\",\"jigou\":\""+jigou+"\"}]";
			     callserver(list_user_jsonObj, sucess_search_user, error);
			}
			//查询组中用户
			function search_group_user(){
			 var user_name=$("#group_user_query").attr("value");
			 var jigou=$("#group_user_query_jigou").attr("value");
			     group_user_jsonObj.extenddata = "[{\"id\":\""+check_group_id+"\",\"userName\":\""+user_name+"\",\"jigou\":\""+jigou+"\"}]";
			     callserver(group_user_jsonObj, sucess_search_guser, error);
			}
			//////////////////////////////////通用方法//////////////////////////////////////////////////
			//隐藏窗口
			function closeWin(item, dialog) {
				dialog.hide();
			}
			function close_userdiv(item, dialog) {
				user_group_dialog.hide();
			}
			function close_rolediv(item, dialog) {
				role_group_dialog.hide();
			}
			//总回调函数
			function sucess(data){
			    succMsgTip(data.resultrecord)
			    reload();
			}
			function error(data){
			    warningTip('很遗憾，操作失败！')
			    reload();
			}
			//组选人用：回调函数
			function sucess_guser(data){
			    succMsgTip(data.resultrecord)
			     group_user.loadData();
			     check_users.loadData();
			}
			function sucess_search_user(data){
			    group_user.loadData();
			}
			function sucess_search_guser(data){
			    check_users.loadData();
			}
			//组选角色用：回调函数
			function sucess_grole(data){
			  succMsgTip(data.resultrecord)
			    group_role.loadData();
			    check_roles.loadData();
			}
			function sucess_search_role(data){
			    group_role.loadData();
			}
			function sucess_search_grole(data){
			    check_roles.loadData();
			}
  </script>
  <body>
   <div id="group-grid"></div>
    <div  style="width:100%;height:100%;display:none" id="goods" >
			<form>
				<table  class="l-table-edit">
					<tr>
						<td><input type="hidden" name="id"  id="id" width="150"></td>
					</tr>
					<tr >
						<td align="right" class="l-table-edit-td">父级名称：</td>
						<td align="left" class="l-table-edit-td" style="padding-left: 3px;">
							<input class="textbg1" type="text" name="pname" id="pname" style="width: 240px;" readonly="readonly">
							<input type="hidden" name="pid" id="pid" readonly="readonly">
					    </td>
					</tr>
					<tr>
						<td align="right" class="l-table-edit-td">名称：</td>
						<td align="left" class="l-table-edit-td" style="padding-left: 3px;">
						    <input class="textbg1" type="text" name="caption" id="caption" style="width: 240px;">
						</td>
					</tr>
					<tr >
						<td align="right" class="l-table-edit-td">排序号：</td>
						<td align="left" class="l-table-edit-td" style="padding-left: 3px;">
						    <input class="textbg1" type="text" name="group_code" id="group_code" style="width: 240px;">
					    </td>
					</tr>
				</table>
			</form>			
		</div>
<!-- 组与人的左右选择 -->
        <div id="group_user" name="group_user">
			<table border="0" >
				<tr>
					<td> 
					    <div align="left">
						  用户：<input id="group_user_query"></input><br/>
						  机构：<input id="group_user_query_jigou"></input>  
						 <input class="btn1" id="query" type="button" value="查询" onclick="search_group_user();"/> </div>
					   <div id="check_user"></div>
					</td>
					<td align="center">
						<input type="button" value="选择" class="btn1" onclick="save_group_user();">
						  <br> <br> <br> <br> <br> <br> <br> <br>
						<input type="button" value="移除" class="btn1" onclick="del_group_user();">
				   </td>
					<td >
					 <div >
						  用户：<input id="user_query"></input><br/>
						  机构：<input id="user_query_jigou"></input>  
						 <input class="btn1" id="query" type="button" value="查询" onclick="serach_user();"/> </div>
						<div id="user_div"></div>
					</td>
				</tr>
			</table>
		</div>
<!-- 组与角色的左右选择 -->
        <div id="group_role" name="group_role">
			<table border="0" >
				<tr>
					<td> 
					<div>
						    名称：<input class="textbg1" id="group_role_name"></input>  
						 <input class="btn1" id="query" type="button" value="查询" onclick="search_group_role();"/> 
						 </div>
					   <div id="check_role"></div>
					</td>
					<td align="center">
						<input class="btn1" type="button" value="选择" onclick="save_group_role();">
						  <br> <br> <br> <br>  <br> <br> <br> <br>
						<input class="btn1" type="button" value="移除 " onclick="del_group_role();">
				   </td>
					<td >
					     <div>
						    名称：<input class="textbg1" id="role_name"></input>  
						 <input class="btn1" id="query" type="button" value="查询"onclick="search_role();"/> 
						 </div>
						<div id="role_div"></div>
					</td>
				</tr>
			</table>
		</div>
  </body>
</html>
